﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class AdminController : Controller
    {
        private AdminBLO _adminBlo = new AdminBLO();
        private TheaterBLO _theaterBlo = new TheaterBLO();

        public ActionResult Index()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            List<Theater> listTheater = new List<Theater>();
            listTheater = _theaterBlo.GetAllTheaterInfo();
            List<AdminHomeInfoConcertEntity> listScheduleOverCurrentDay = new List<AdminHomeInfoConcertEntity>();
            List<AdminHomeInfoConcertEntity> listScheduleNeedToCreateTicket = new List<AdminHomeInfoConcertEntity>();
            foreach (var theater in listTheater)
            {
                //Get list schedule over current day
                AdminHomeInfoConcertEntity adminHomeInfor = new AdminHomeInfoConcertEntity();
                adminHomeInfor = _adminBlo.DeleteAllShowTimeAndScheduleAndTicketOverCurrentDay(theater);
                adminHomeInfor.TheaterId = theater.TheaterId;
                listScheduleOverCurrentDay.Add(adminHomeInfor);

                //Get list schedule need to create ticket, because this schedule have Date = current Day + deadline for create Ticket
                AdminHomeInfoConcertEntity scheduleNeedToCreateTicket = new AdminHomeInfoConcertEntity();
                scheduleNeedToCreateTicket = _adminBlo.GetListHaveToCreateTicket(theater);
                listScheduleNeedToCreateTicket.Add(scheduleNeedToCreateTicket);
            }

            AdminHomeInfoConcertEntity listPromotionOverDay = new AdminHomeInfoConcertEntity();
            //Delete all promotion over current day
            listPromotionOverDay = _adminBlo.DeleteAllPromotionOverDay();

            //Check have any schedule over current day
            bool isHasScheduleOverCurrentDay = false;
            foreach (var item in listScheduleOverCurrentDay)
            {
                if (item.TotalScheduleDelete > 0)
                {
                    isHasScheduleOverCurrentDay = true;
                    break;
                }
            }

            //Check have any schedule have to create ticket
            var isHasScheduleNeedToCreateTicket = false;
            foreach (var item in listScheduleNeedToCreateTicket)
            {
                if (item.TotalScheduleDelete > 0)
                {
                    isHasScheduleNeedToCreateTicket = true;
                }
            }


            bool isHasPromotionOverDay = false;
            if (listPromotionOverDay.TotalPromotionDelete > 0)
            {
                isHasPromotionOverDay = true;
            }


            ViewBag.ListAllAdminHomeInfor = listScheduleOverCurrentDay;
            ViewBag.IsHasScheduleDelete = isHasScheduleOverCurrentDay;

            ViewBag.ListScheduleNeedToCreateTicket = listScheduleNeedToCreateTicket;
            ViewBag.IsHasScheduleNeedToCreateTicket = isHasScheduleNeedToCreateTicket;

            ViewBag.ListPromotionOverDay = listPromotionOverDay;
            ViewBag.IsHasPromotionOverDay = isHasPromotionOverDay;
            return View();
        }

        /// <summary>
        /// Staff page
        /// </summary>
        /// <returns></returns>
        public ActionResult StaffPage()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<Theater> listTheater = new List<Theater>();
            listTheater = _theaterBlo.GetAllTheaterInfo();
            List<AdminHomeInfoConcertEntity> listScheduleOverCurrentDay = new List<AdminHomeInfoConcertEntity>();
            List<AdminHomeInfoConcertEntity> listScheduleNeedToCreateTicket = new List<AdminHomeInfoConcertEntity>();
            List<AdminHomeInfoConcertEntity> listPaymentNeedToConfirm = new List<AdminHomeInfoConcertEntity>();
            foreach (var theater in listTheater)
            {
                //Get list schedule over current day
                AdminHomeInfoConcertEntity adminHomeInfor = new AdminHomeInfoConcertEntity();
                adminHomeInfor = _adminBlo.DeleteAllShowTimeAndScheduleAndTicketOverCurrentDay(theater);
                adminHomeInfor.TheaterId = theater.TheaterId;
                listScheduleOverCurrentDay.Add(adminHomeInfor);

                //Get list schedule need to create ticket, because this schedule have Date = current Day + deadline for create Ticket
                AdminHomeInfoConcertEntity scheduleNeedToCreateTicket = new AdminHomeInfoConcertEntity();
                scheduleNeedToCreateTicket = _adminBlo.GetListHaveToCreateTicket(theater);
                listScheduleNeedToCreateTicket.Add(scheduleNeedToCreateTicket);
            }

            //Get list payment need to confirm
            AdminHomeInfoConcertEntity paymentConfirm = new AdminHomeInfoConcertEntity();
            paymentConfirm = _adminBlo.GetListPaymentHaveToConfirm();
            bool isHasPaymentConfirm = false;
            if (paymentConfirm.ListPaymentConfirm.Count() > 0)
            {
                isHasPaymentConfirm = true;
            }

            AdminHomeInfoConcertEntity listPromotionOverDay = new AdminHomeInfoConcertEntity();
            //Delete all promotion over current day
            listPromotionOverDay = _adminBlo.DeleteAllPromotionOverDay();

            //Check have any schedule over current day
            bool isHasScheduleOverCurrentDay = false;
            foreach (var item in listScheduleOverCurrentDay)
            {
                if (item.TotalScheduleDelete > 0)
                {
                    isHasScheduleOverCurrentDay = true;
                    break;
                }
            }

            //Check have any schedule have to create ticket
            var isHasScheduleNeedToCreateTicket = false;
            foreach (var item in listScheduleNeedToCreateTicket)
            {
                if (item.TotalScheduleDelete > 0)
                {
                    isHasScheduleNeedToCreateTicket = true;
                }
            }

            //Check have any payment have to confirm
            bool isHasPromotionOverDay = false;
            if (listPromotionOverDay.TotalPromotionDelete > 0)
            {
                isHasPromotionOverDay = true;
            }


            ViewBag.ListAllAdminHomeInfor = listScheduleOverCurrentDay;
            ViewBag.IsHasScheduleDelete = isHasScheduleOverCurrentDay;

            ViewBag.ListScheduleNeedToCreateTicket = listScheduleNeedToCreateTicket;
            ViewBag.IsHasScheduleNeedToCreateTicket = isHasScheduleNeedToCreateTicket;

            ViewBag.ListPromotionOverDay = listPromotionOverDay;
            ViewBag.IsHasPromotionOverDay = isHasPromotionOverDay;

            ViewBag.ListPaymentConfirm = paymentConfirm;
            ViewBag.IsHasPaymentConfirm = isHasPaymentConfirm;

            return View();
        }

        public ActionResult SupplierPage()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            List<Theater> listTheater = new List<Theater>();
            listTheater = _theaterBlo.GetAllTheaterInfo();
            List<AdminHomeInfoConcertEntity> listAllAdminHome = new List<AdminHomeInfoConcertEntity>();
            foreach (var theater in listTheater)
            {
                AdminHomeInfoConcertEntity adminHomeInfor = new AdminHomeInfoConcertEntity();
                adminHomeInfor = _adminBlo.DeleteAllShowTimeAndScheduleAndTicketOverCurrentDay(theater);
                adminHomeInfor.TheaterId = theater.TheaterId;
                listAllAdminHome.Add(adminHomeInfor);
            }
            var resultDeletePromotion = _adminBlo.DeleteAllPromotionOverDay();
            ViewBag.ListAllAdminHomeInfor = listAllAdminHome;
            return View();
        }

        public ActionResult ManageAnother()
        {
            return View();
        }

        public ActionResult CreateATicket(int scheduleId)
        {
            return RedirectToAction("CreateTickets", "Ticket", scheduleId);
        }
    }
}
